/*
本地模拟接口请求, 仅demo演示用.
实际项目以您服务器接口返回的数据为准,无需本地处理分页.
请参考官方写法: https://www.mescroll.com/uni.html?v=20200210#tagUpCallback
* */

// 模拟数据
import BOSS_label from "./data/BOSS_label.js";
import BOSS_job from "./data/BOSS_job.js";

export const getLabel = () => {
	return new Promise((resolute, reject) => {

		try {
			let list = []
			// console.log(BOSS_label)

			for (let i = 0; i < BOSS_label.length; i++) {
				// console.log(BOSS_label[i].name)
				list.push(BOSS_label[i])
			}

			// list.push(BOSS_label)

			// console.log(list)
			resolute(list)
		} catch (e) {
			//TODO handle the exception
			reject(e)
		}


	})
}

// 搜索商品
export const getList = (name, pageNum, pageSize) => {
	return new Promise((resolute, reject) => {
		//延时一秒,模拟联网
		setTimeout(() => {
			try {
				// 模拟搜索
				let list = []
				if (!name || name == "全部") {
					// 模拟搜索全部商品
					for (let i = (pageNum - 1) * pageSize; i < pageNum * pageSize; i++) {
						if (i === BOSS_job.length) break
						list.push(BOSS_job[i])
					}
				} else {
					// 模拟关键词搜索
					if (name == "后端开发") name = "后端"; // 为这个关键词展示多几条数据
					for (let i = 0; i < BOSS_job.length; i++) {
						if (BOSS_job[i].classify.indexOf(name) !== -1) {
							list.push(BOSS_job[i])
						}
					}
				}
				//模拟接口请求成功
				// console.log(list)
				resolute(list);
			} catch (e) {
				//模拟接口请求失败
				reject(e);
			}
		}, 1000)
	})
}

export const getDetail = (id) => {
	return new Promise((resolve, reject) => {
		try {
			//模拟接口请求成功
			const result = BOSS_job.find(item => item._id.$oid == id)
			resolve(result);
		} catch (e) {
			//模拟接口请求失败
			reject(e);
		}
	})
}
